package com.hcryan.djm.service.impl;

import com.hcryan.djm.config.BaiduMapConfig;
import com.hcryan.djm.exception.ErrorMessage;
import com.hcryan.djm.exception.TravelException;
import com.hcryan.djm.feign.BaiduMapApi;
import com.hcryan.djm.feign.response.DrivingResp;
import com.hcryan.djm.service.BaiduMapService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ryanwdj
 * @since 2025/2/23
 */
@Service
public class BaiduMapServiceImpl implements BaiduMapService {

    @Autowired
    private BaiduMapApi baiduMapApi;
    @Autowired
    private BaiduMapConfig baiduMapConfig;

    @Override
    public DrivingResp searchDriving(String origin, String waypoints, String destination) {

        DrivingResp resp ;
        if(StringUtils.isEmpty(waypoints)){
            resp = baiduMapApi.drivingSearch(baiduMapConfig.getAk(), origin, destination);
        }else{
            String[] split = waypoints.split("\\|");
            int drivingWaypointRestrict = 10;
            if(split.length > drivingWaypointRestrict){
                throw new TravelException(ErrorMessage.ERROR, "途经点需小于10个");
            }

            String wayPointsStr = String.join("|", waypoints);
            resp = baiduMapApi.drivingSearch(baiduMapConfig.getAk(), origin, wayPointsStr, destination);
        }
        return resp;
    }
}
